﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.BlackApart.Web;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Business.Prices;
using blackhouse.JavaScript.JQuery;
using blackhouse.Aparts.Web.Modules;
using blackhouse.BlackApart.Web.Navigation;

namespace blackhouse.Aparts.Web.Pages {
    public partial class ShowReservationPopup : System.Web.UI.Page {

        #region Fields

        private Factory factory;
        private SearchState state;
        private IApartmentGroup group;
        private Variant variant;
        private DisplayReservationPopupMode mode = DisplayReservationPopupMode.ForMakeReservation;

        #endregion

        #region Event Handlers

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            this.onlineReservation.SeachValueChanged += new SearchValueChangedEventHandlers(onlineReservation_SeachValueChanged);
            this.SetInstance();
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.AddJavascript();
            this.PopulateControls();
        }

        #endregion

        #region Methods

        private void PopulateControls() {
            this.onlineReservation.PeoplesCount = this.group.NumberOfPeople;

            if (this.mode == DisplayReservationPopupMode.ForMakeReservation)
                this.PopulateForReservation();
            if (this.mode == DisplayReservationPopupMode.ForChangeParams)
                this.PopulateForChangeParams();
        }

        private void PopulateForChangeParams() {
            this.pnlLoader.Visible = false;
            this.ltlChangeParams.Visible = true;
            this.Page.Form.Target = "_parent";
            this.onlineReservation.ChangeButtonText("zmień");
        }

        private void PopulateForReservation() {
            this.ltlStart.Visible = true;

            if (this.state.IsSet && variant != null)
                this.PopulateApartmentFree();

            if (this.state.IsSet && variant == null)
                this.PopulateApartmentNotFree();
        }

        private void PopulateApartmentFree() {
            this.ltlStart.Visible = false;
            this.ltlApartmentFree.Visible = true;
            this.pnlPrice.Visible = true;
            this.lblPrice.Value = this.variant.Price.GetPricePerNight() / this.state.PeopleCount;
            this.lblPriceAll.Value = this.variant.Price.GetPrice();
            this.lnkToReservation.NavigateUrl = NavigationProvider.Instance().GetUrl(ReservationPage.GetInformationPage(this.group.Id));
            this.lnkToReservation.ToolTip = "Zarezerwuj apartament - " + this.group.Name;
            this.lnkToReservation.Visible = true;
            this.lnkToAllApartments.NavigateUrl = NavigationProvider.Instance().GetUrl(this.state);
            this.lnkToAllApartments.ToolTip = "Pokaż wszystkie apartamenty dla wybranego terminu";
            this.lnkToAllApartments.Visible = true;

            int nights = (int)(this.state.DepartureDate.Date - this.state.ArrivalDate.Date).TotalDays;
            string nightText = nights + " ";
            if (nights == 1)
                nightText += GetGlobalResourceObject("Aparts", "nights1");
            if (nights > 1 && nights <= 4)
                nightText += GetGlobalResourceObject("Aparts", "nights24");
            if (nights > 4)
                nightText += GetGlobalResourceObject("Aparts", "nightsmore");
            this.ltlNights.Text = nightText;
        }

        private void PopulateApartmentNotFree() {
            this.ltlStart.Visible = false;
            this.ltlApartmentNotFree.Visible = true;
            this.lnkToAllApartments.NavigateUrl = NavigationProvider.Instance().GetUrl(this.state);
            this.lnkToAllApartments.ToolTip = "Pokaż wszystkie apartamenty dla wybranego terminu";
            this.lnkToAllApartments.Visible = true;
        }

        private void SetInstance() {
            this.factory = Factory.Instance();
            this.state = SearchState.GetFromContext();

            int groupId = 0;
            Int32.TryParse(Request.QueryString["groupId"], out groupId);
            this.group = factory.GetApartmentGroup(groupId);

            int mod = 0;
            if (Int32.TryParse(Request.QueryString["mod"], out mod) && Enum.IsDefined(typeof(DisplayReservationPopupMode), mod)) {
                this.mode = (DisplayReservationPopupMode)mod;
            }

            if (this.mode == DisplayReservationPopupMode.ForMakeReservation && state.IsSet) {

                FreeApartments search = this.state.GetSearchVariants();
                if (this.group != null)
                    search.FilterToApartmentType(this.group.Id);
                if (search.Count() > 0)
                    this.variant = search.First();
                if (this.group != null) {
                    this.lnkToApartment.Visible = true;
                    this.lnkToApartment.Attributes.Add("rel", "nofollow");
                    this.lnkToApartment.ToolTip = this.group.Name;
                    this.lnkToApartment.NavigateUrl = UrlUtils.GetApartmentTypeUrl(this.group, DisplayApartmentTypeView.Default, 1, this.state);
                }
            }
        }

        protected void onlineReservation_SeachValueChanged(object sender, SearchChangedEventArgs args) {
            if (this.mode == DisplayReservationPopupMode.ForMakeReservation)
                Response.Redirect(string.Format(UrlUtils.LocalizeUrl("/pages/showreservationpopup.aspx") + "?groupId={0}&search={1}", this.group.Id, SearchState.GetStringState(args.ArrivalDate, args.DepartureDate, args.PeopleCount)));
            if (this.mode == DisplayReservationPopupMode.ForChangeParams)
                Response.Redirect(UrlUtils.GetApartmentTypeUrl(this.group, DisplayApartmentTypeView.Default, 1, args.GetState()));

        }

        private void AddJavascript() {
            JQueryLibraries jql = new JQueryLibraries();
            jql.IncludeFileBasedOnJQuery("blackhouse.Aparts.Web.Scripts.reservation-popup.js,blackhouse.Aparts.Web");
        }

        #endregion

    }
}